package org.dromara.datacheck.service.impl.jst;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.datacheck.domain.bo.JstAndJDTableDataEntryBo;
import org.dromara.datacheck.domain.bo.jst.ConsignmentBusinessDifferenceBo;
import org.dromara.datacheck.domain.bo.jst.JstOtherOutEnterWarehouseOrderBo;
import org.dromara.datacheck.domain.jd.JdDirectTransferOrder;
import org.dromara.datacheck.domain.jd.JdOtherEnterWarehouseOrder;
import org.dromara.datacheck.domain.jd.JdOtherOutWarehouseOrder;
import org.dromara.datacheck.domain.jst.JstOtherOutEnterWarehouseOrder;
import org.dromara.datacheck.domain.vo.jd.JdDirectTransferOrderVo;
import org.dromara.datacheck.domain.vo.jd.JdOtherEnterWarehouseOrderVo;
import org.dromara.datacheck.domain.vo.jd.JdOtherOutWarehouseOrderVo;
import org.dromara.datacheck.domain.vo.jst.JstOtherOutEnterWarehouseOrderVo;
import org.dromara.datacheck.mapper.jd.JdDirectTransferOrderMapper;
import org.dromara.datacheck.mapper.jd.JdOtherEnterWarehouseOrderMapper;
import org.dromara.datacheck.mapper.jd.JdOtherOutWarehouseOrderMapper;
import org.dromara.datacheck.mapper.jst.JstOtherOutEnterWarehouseOrderMapper;
import org.dromara.datacheck.service.jd.IJdOtherOutWarehouseOrderService;
import org.dromara.datacheck.service.jst.IJstOtherOutEnterWarehouseOrderService;
import org.dromara.datacheck.util.MyUtil;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 聚水潭-其他出入库单Service业务层处理
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@RequiredArgsConstructor
@Service
public class JstOtherOutEnterWarehouseOrderServiceImpl implements IJstOtherOutEnterWarehouseOrderService {

    private final JstOtherOutEnterWarehouseOrderMapper baseMapper;

    /**
     * 金蝶-其他出库单Service接口
     */
    @Resource
    private IJdOtherOutWarehouseOrderService jdOtherOutWarehouseOrderService;

    @Resource
    private JdOtherOutWarehouseOrderMapper jdOtherOutWarehouseOrderMapper;

    @Resource
    private JdOtherEnterWarehouseOrderMapper jdOtherEnterWarehouseOrderMapper;

    @Resource
    private JdDirectTransferOrderMapper jdDirectTransferOrderMapper;

    /**
     * 查询聚水潭-其他出入库单
     *
     * @param id 主键
     * @return 聚水潭-其他出入库单
     */
    @Override
    public JstOtherOutEnterWarehouseOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询聚水潭-其他出入库单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 聚水潭-其他出入库单分页列表
     */
    @Override
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> queryPageList(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapper(bo);
        Page<JstOtherOutEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的聚水潭-其他出入库单列表
     *
     * @param bo 查询条件
     * @return 聚水潭-其他出入库单列表
     */
    @Override
    public List<JstOtherOutEnterWarehouseOrderVo> queryList(JstOtherOutEnterWarehouseOrderBo bo) {
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 构建查询包装器
     * 该方法用于根据业务对象（bo）的属性构建一个LambdaQueryWrapper对象，用于数据库查询条件构造
     * 通过该方法，可以动态地根据业务对象的属性值设置查询条件，从而提高查询的灵活性和代码的可读性
     *
     * @param bo 业务对象，包含查询所需的业务数据和参数
     * @return LambdaQueryWrapper用于执行数据库查询的条件包装器
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapper(JstOtherOutEnterWarehouseOrderBo bo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }
        // 返回构建好的查询条件包装器
        return lqw;
    }


    /**
     * 新增聚水潭-其他出入库单
     *
     * @param bo 聚水潭-其他出入库单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(JstOtherOutEnterWarehouseOrderBo bo) {
        JstOtherOutEnterWarehouseOrder add = MapstructUtils.convert(bo, JstOtherOutEnterWarehouseOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(String.valueOf(add.getId()));
        }
        return flag;
    }

    /**
     * 修改聚水潭-其他出入库单
     *
     * @param bo 聚水潭-其他出入库单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(JstOtherOutEnterWarehouseOrderBo bo) {
        JstOtherOutEnterWarehouseOrder update = MapstructUtils.convert(bo, JstOtherOutEnterWarehouseOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JstOtherOutEnterWarehouseOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除聚水潭-其他出入库单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 查询聚水潭-其他出入库单列表所有数据
     *
     * @return 聚水潭-其他出入库单列表
     */
    @Override
    public List<JstOtherOutEnterWarehouseOrder> queryAll() {
        return baseMapper.selectList(null);
    }

    /**
     * 清空聚水潭-其他出入库单列表所有数据
     *
     * @return 聚水潭-其他出入库单列表
     */
    @Override
    public Integer deleteAll() {
        return baseMapper.delete(new QueryWrapper<>());
    }

    /**
     * 查询聚水潭-其他出入库单列表
     * 场景分类：其他出库-报废，盘亏
     * 业务查询条件：根据out_warehouse_type出库类型为报废，盘亏查询
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> queryPageListByOutWareHouseType1(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperByOutWareHouseType1(bo);
        Page<JstOtherOutEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 构建查询包装器
     * 该方法用于根据业务对象（bo）的属性构建一个LambdaQueryWrapper对象，用于数据库查询条件构造
     * 通过该方法，可以动态地根据业务对象的属性值设置查询条件，从而提高查询的灵活性和代码的可读性
     * 场景分类：其他出库-报废，盘亏
     * 业务查询条件：根据out_warehouse_type出库类型为报废，盘亏查询
     *
     * @param bo 业务对象，包含查询所需的业务数据和参数
     * @return LambdaQueryWrapper用于执行数据库查询的条件包装器
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperByOutWareHouseType1(JstOtherOutEnterWarehouseOrderBo bo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        // 出库类型
        lqw.in(CollUtil.isNotEmpty(bo.getOutWarehouseTypeList()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseTypeList());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }
        // 返回构建好的查询条件包装器
        return lqw;
    }

    /**
     * 查询聚水潭-其他出入库单列表
     * 场景分类：其他出库-供应商赔付，盘盈，领用退回
     * 业务查询条件：根据out_warehouse_type出库类型为供应商赔付，盘盈，领用退回查询
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> queryPageListByOutWareHouseType2(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperByOutWareHouseType2(bo);
        Page<JstOtherOutEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 构建查询包装器
     * 该方法用于根据业务对象（bo）的属性构建一个LambdaQueryWrapper对象，用于数据库查询条件构造
     * 通过该方法，可以动态地根据业务对象的属性值设置查询条件，从而提高查询的灵活性和代码的可读性
     * 场景分类：其他出库-供应商赔付，盘盈，领用退回
     * 业务查询条件：根据out_warehouse_type出库类型为供应商赔付，盘盈，领用退回查询
     *
     * @param bo 业务对象，包含查询所需的业务数据和参数
     * @return LambdaQueryWrapper用于执行数据库查询的条件包装器
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperByOutWareHouseType2(JstOtherOutEnterWarehouseOrderBo bo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        // 出库类型
        lqw.in(CollUtil.isNotEmpty(bo.getOutWarehouseTypeList()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseTypeList());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }
        // 返回构建好的查询条件包装器
        return lqw;
    }

    /**
     * 查询聚水潭-其他出入库单列表
     * 场景分类：调拨-销退仓,次品仓调拨委外加工仓
     * 业务查询条件：根据out_warehouse_type出库类型为仓库编码+仓库的单子查询
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> queryPageListByOutWareHouseType3(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperByOutWareHouseType3(bo);
        Page<JstOtherOutEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 构建查询包装器
     * 该方法用于根据业务对象（bo）的属性构建一个LambdaQueryWrapper对象，用于数据库查询条件构造
     * 通过该方法，可以动态地根据业务对象的属性值设置查询条件，从而提高查询的灵活性和代码的可读性
     * 场景分类：调拨-销退仓,次品仓调拨委外加工仓
     * 业务查询条件：根据out_warehouse_type出库类型为仓库编码+仓库的单子查询
     *
     * @param bo 业务对象，包含查询所需的业务数据和参数
     * @return LambdaQueryWrapper用于执行数据库查询的条件包装器
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperByOutWareHouseType3(JstOtherOutEnterWarehouseOrderBo bo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        // 出库类型，查询出库类型字段形如“仓库编码+仓库”的单子，其中仓库编码以“CK”开头
        lqw.like(JstOtherOutEnterWarehouseOrder::getOutWarehouseType, "%CK%");
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }

        // 返回构建好的查询条件包装器
        return lqw;
    }

    /**
     * 查询聚水潭-其他出入库单列表和金蝶其他出库的数据汇总
     * 按日期汇总的统计数据
     */
    @Override
    public TableDataInfo<Map<String, Object>> jstAndJdSum() {
        // 查询聚水潭的数据
        List<JstOtherOutEnterWarehouseOrder> jstOtherOutEnterWarhouseOrderList = baseMapper.selectList(null);

        // 查询金蝶的数据
        List<JdOtherOutWarehouseOrder> jdOtherOutWarehouseOrderList = jdOtherOutWarehouseOrderService.queryAll();

        // 生成日期列表，以聚水潭的最晚日期为截止日期取
        List<String> dateList = jstOtherOutEnterWarhouseOrderList.stream()
                .map(JstOtherOutEnterWarehouseOrder::getBillDate)
                .distinct()
                .collect(Collectors.toList());

        // 获取去重后的日期列表，并转换格式
        Set<String> formattedDateSet = jstOtherOutEnterWarhouseOrderList.stream()
            // 提取日期字符串
            .map(JstOtherOutEnterWarehouseOrder::getBillDate)
            // 格式化日期字符串
            .map(this::formatDateString)
            // 使用 Set 去重
            .collect(Collectors.toSet());
        List<String> formattedDateList = new ArrayList<>(formattedDateSet);

        // 处理聚水潭和金蝶的数据里面的日期
        Map<String, Integer> jstCountMap = jstOtherOutEnterWarhouseOrderList.stream()
            .collect(Collectors.groupingBy(JstOtherOutEnterWarehouseOrder::getBillDate, Collectors.summingInt(o -> 1)));

        // TODO这里要改的
        Map<String, Integer> jdCountMap = jdOtherOutWarehouseOrderList.stream()
            .collect(Collectors.groupingBy(JdOtherOutWarehouseOrder::getUnitName, Collectors.summingInt(o -> 1)));

        // 合并两个Map，并计算数量之和和差异
        Map<String, Map<String, Integer>> combinedMap = new HashMap<>();
        for (String date : formattedDateList) {
            int jstCount = jstCountMap.getOrDefault(formatDateString(date), 0);
            int jdCount = jdCountMap.getOrDefault(formatDateString(date), 0);
            int total = jstCount + jdCount;
            int diff = Math.abs(jstCount - jdCount);

            combinedMap.put(date, new HashMap<>() {{
                put("jstCount", jstCount);
                put("jdCount", jdCount);
                put("total", total);
                put("diff", diff);
            }});
        }

        // 构建JSON
        TableDataInfo<Map<String, Object>> result = new TableDataInfo<>();
//        result.setRows(combinedMap.values().stream().collect(Collectors.toList()));

        return result;
    }


    public String formatDateString(String dateStr) {
        // 分割日期字符串
        String[] parts = dateStr.split(" ")[0].split("/");

        // 构造新的日期字符串
        return parts[0] + "年" + parts[1] + "月" + parts[2] + "日";
    }

    /**
     * 查询聚水潭-其他出库单列表以及金蝶-其他出库单数据汇总（按日期汇总）
     * 场景分类：6、其他出库-报废，盘亏
     */
    @Override
    public TableDataInfo<JstAndJDTableDataEntryBo> jstAndJdSumByDateByScrapInventory() {
        List<JstAndJDTableDataEntryBo> jstAndJDTableDataEntryList =new ArrayList<>();
        JstAndJDTableDataEntryBo jstAndJDTableDataEntry = new JstAndJDTableDataEntryBo();
        // 1、查询两个平台数据
        // 1-1、查询聚水潭的数据
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw1 = Wrappers.lambdaQuery();
        lqw1.in(JstOtherOutEnterWarehouseOrder::getOutWarehouseType, Arrays.asList("报废","盘亏"));
        lqw1.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        List<JstOtherOutEnterWarehouseOrderVo> jstOtherOutEnterWarehouseOrderList = baseMapper.selectVoList(lqw1);

        // 1-2、查询金蝶的数据
        LambdaQueryWrapper<JdOtherOutWarehouseOrder> lqw2 = Wrappers.lambdaQuery();
        lqw2.eq(JdOtherOutWarehouseOrder::getJdOtherOutWarehouseOrderNumber, "%QT");
        List<JdOtherOutWarehouseOrderVo> jdOtherOutWarehouseOrderList = jdOtherOutWarehouseOrderMapper.selectVoList(lqw2);

        // 2、生成日期列表和处理日期
        // 2-1、生成日期列表，以聚水潭的最晚日期为截止日期取
        List<Date> dateList = jstOtherOutEnterWarehouseOrderList.stream()
            .sorted(Comparator.comparing(JstOtherOutEnterWarehouseOrderVo::getNewOrderDate))
            .map(JstOtherOutEnterWarehouseOrderVo::getNewOrderDate)
            .distinct()
            .collect(Collectors.toList());

        // 2-2、将日期列表转换为字符串列表
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        List<String> dateStringList = dateList.stream()
            .map(date -> dateFormat.format(date))
            .collect(Collectors.toList());

        // 2-3、获取去重后的日期列表，并转换格式
        Set<String> formattedDateSet = dateStringList.stream()
            // 将字符串转换为 LocalDate 对象
            .map(MyUtil::parseToLocalDate)
            .sorted()
            .map(MyUtil::formatDateStringByLocalDate)
            .collect(Collectors.toSet());
        List<String> formattedDateList = new ArrayList<>(formattedDateSet);
        Collections.sort(formattedDateList, Comparator.naturalOrder());

        // 3、处理两个平台的日期
        // 3-1、处理聚水潭和金蝶的数据里面的日期
        // key 为日期，value 为数量
        Map<String, Integer> jstCountMap = jstOtherOutEnterWarehouseOrderList.stream()
            .filter(order -> order.getNewOrderDate() != null)
            .sorted(Comparator.comparing(JstOtherOutEnterWarehouseOrderVo::getNewOrderDate))
            .collect(Collectors.groupingBy(
                order -> MyUtil.formatDateStringNoPadZero(dateFormat.format(order.getNewOrderDate())),
                Collectors.summingInt(JstOtherOutEnterWarehouseOrderVo::getQuantity)
            ));

        // 3-2、处理金蝶的数据里面的日期
        // key 为日期，value 为数量
        Map<String, Integer> jdCountMap = jdOtherOutWarehouseOrderList.stream()
            .filter(order -> order.getOrderDate() != null)
            .sorted(Comparator.comparing(JdOtherOutWarehouseOrderVo::getOrderDate))
            .collect(Collectors.groupingBy(
                order -> MyUtil.formatDateStringPadZero(order.getOrderDate()),
                Collectors.summingInt(JdOtherOutWarehouseOrderVo::getActualDeliveryNumber)
            ));

        // 4、构建形如<date1,日期>的key value对，这个是前端要求
        Map<String, String> dateMap = MyUtil.buildDateMap(formattedDateList);

        // 5、构建表格数据
        // 数据格式形如: "tableList": [
        //                {
        //                    "label": "金蝶",
        //                    "date1": 0,
        //                    "date2": 2,
        //                    "date3": 3420,
        //                    "date4": 179,
        //                    "date5": 449,
        //                    "date6": 922,
        //                    "date7": 0,
        //                    "date8": 1822,
        //                    "date9": 0
        //                },
        //                {
        //                    "label": "聚水潭",
        //                    "date1": 2130,
        //                    "date2": 2,
        //                    "date3": 3420,
        //                    "date4": 179,
        //                    "date5": 449,
        //                    "date6": 922,
        //                    "date7": 1409,
        //                    "date8": 1822,
        //                    "date9": 1631
        //                },
        //                {
        //                    "label": "差异",
        //                    "date1": 2130,
        //                    "date2": 0,
        //                    "date3": 0,
        //                    "date4": 0,
        //                    "date5": 0,
        //                    "date6": 0,
        //                    "date7": 1409,
        //                    "date8": 0,
        //                    "date9": 1631
        //                }
        //            ]
        List<Map<String, Object>> resultList = new ArrayList<>();

        // 5-1、初始化三个 Map 来存储数据
        Map<String, Object> jdData = new LinkedHashMap<>();
        Map<String, Object> jstData = new LinkedHashMap<>();
        Map<String, Object> diffData = new LinkedHashMap<>();

        // 设置 label
        jdData.put("label", "金蝶");
        jstData.put("label", "聚水潭");
        diffData.put("label", "差异");

        // 5-2、遍历日期列表，填充数据
        int dateIndex = 1;
        for (String date : formattedDateList) {
            int jstCount = jstCountMap.getOrDefault(date, 0);
            int jdCount = jdCountMap.getOrDefault(date, 0);
            int diff = Math.abs(jstCount - jdCount);

            // 填充数据
            jdData.put("date" + dateIndex, jdCount);
            jstData.put("date" + dateIndex, jstCount);
            diffData.put("date" + dateIndex, diff);

            dateIndex++;
        }

        // 5-3、将三个 Map 添加到结果列表中
        resultList.add(jdData);
        resultList.add(jstData);
        resultList.add(diffData);
        // 5-4、设置表格数据
        jstAndJDTableDataEntry.setTableList(resultList);

        // 6、构建列数据
        // 数据格式形如:
        // [
        //                {
        //                    "label": "系统",
        //                    "prop": "label"
        //                },
        //                {
        //                    "label": "2024年07月31日",
        //                    "prop": "date1"
        //                },
        // ]
        List<Map<String, Object>> columnList = new ArrayList<>();
        // 6-1、添加 "系统" 列
        Map<String, Object> systemColumn = new LinkedHashMap<>();
        systemColumn.put("label", "系统");
        systemColumn.put("prop", "label");
        columnList.add(systemColumn);

        // 6-2、添加日期列
        for (int i = 0; i <formattedDateList.size() +1; i++) {
            Map<String, Object> dateColumn = new LinkedHashMap<>();
            // 可以根据需要修改日期格式，根据业务需要，这里先按照date1这样处理
            dateColumn.put("label", dateMap.get("date" + i));
            dateColumn.put("prop", "date" + i);
            columnList.add(dateColumn);
            dateIndex++;
        }
        // 6-3、设置表格数据
        jstAndJDTableDataEntry.setColumnList(columnList);
        jstAndJDTableDataEntryList.add(jstAndJDTableDataEntry);

        // 7、构建JSON，构建返回数据
        TableDataInfo<JstAndJDTableDataEntryBo> result = new TableDataInfo<>();
        result.setRows(jstAndJDTableDataEntryList);
        result.setMsg("查询成功");

        return result;
    }

    /**
     * 查询聚水潭-其他出库单列表以及金蝶-其他出库单数据汇总（按日期汇总）
     * 场景分类：7、其他入库-供应商赔付，盘盈，领用退回
     */
    @Override
    public TableDataInfo<JstAndJDTableDataEntryBo> jstAndJdSumByDateByClaimOrderProfitReturn() {
        List<JstAndJDTableDataEntryBo> jstAndJDTableDataEntryList =new ArrayList<>();
        JstAndJDTableDataEntryBo jstAndJDTableDataEntry = new JstAndJDTableDataEntryBo();
        // 1、查询两个平台数据
        // 1-1、查询聚水潭的数据
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw1 = Wrappers.lambdaQuery();
        lqw1.in(JstOtherOutEnterWarehouseOrder::getOutWarehouseType, Arrays.asList("供应商赔付","盘盈","领用退回"));
        lqw1.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        List<JstOtherOutEnterWarehouseOrderVo> jstOtherOutEnterWarehouseOrderList = baseMapper.selectVoList(lqw1);

        // 1-2、查询金蝶的数据
        LambdaQueryWrapper<JdOtherEnterWarehouseOrder> lqw2 = Wrappers.lambdaQuery();
        lqw2.eq(JdOtherEnterWarehouseOrder::getOtherEnterWarehouseOrderNumber, "%QT");
        List<JdOtherEnterWarehouseOrderVo> jdOtherEnterWarehouseOrderList = jdOtherEnterWarehouseOrderMapper.selectVoList(lqw2);

        // 2、生成日期列表和处理日期
        // 2-1、生成日期列表，以聚水潭的最晚日期为截止日期取
        List<Date> dateList = jstOtherOutEnterWarehouseOrderList.stream()
            .sorted(Comparator.comparing(JstOtherOutEnterWarehouseOrderVo::getNewOrderDate))
            .map(JstOtherOutEnterWarehouseOrderVo::getNewOrderDate)
            .distinct()
            .collect(Collectors.toList());

        // 2-2、将日期列表转换为字符串列表
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        List<String> dateStringList = dateList.stream()
            .map(date -> dateFormat.format(date))
            .collect(Collectors.toList());

        // 2-3、获取去重后的日期列表，并转换格式
        Set<String> formattedDateSet = dateStringList.stream()
            // 将字符串转换为 LocalDate 对象
            .map(MyUtil::parseToLocalDate)
            .sorted()
            .map(MyUtil::formatDateStringByLocalDate)
            .collect(Collectors.toSet());
        List<String> formattedDateList = new ArrayList<>(formattedDateSet);
        Collections.sort(formattedDateList, Comparator.naturalOrder());

        // 3、处理两个平台的日期
        // 3-1、处理聚水潭和金蝶的数据里面的日期
        // key 为日期，value 为数量
        Map<String, Integer> jstCountMap = jstOtherOutEnterWarehouseOrderList.stream()
            .filter(order -> order.getNewOrderDate() != null)
            .sorted(Comparator.comparing(JstOtherOutEnterWarehouseOrderVo::getNewOrderDate))
            .collect(Collectors.groupingBy(
                order -> MyUtil.formatDateStringNoPadZero(dateFormat.format(order.getNewOrderDate())),
                Collectors.summingInt(JstOtherOutEnterWarehouseOrderVo::getQuantity)
            ));

        // 3-2、处理金蝶的数据里面的日期
        // key 为日期，value 为数量
        Map<String, Integer> jdCountMap = jdOtherEnterWarehouseOrderList.stream()
            .filter(order -> order.getOtherEnterWarehouseOrderDate() != null)
            .sorted(Comparator.comparing(JdOtherEnterWarehouseOrderVo::getOtherEnterWarehouseOrderDate))
            .collect(Collectors.groupingBy(
                order -> MyUtil.formatDateStringPadZero(order.getOtherEnterWarehouseOrderDate()),
                Collectors.summingInt(JdOtherEnterWarehouseOrderVo::getActualQuantity)
            ));

        // 4、构建形如<date1,日期>的key value对，这个是前端要求
        Map<String, String> dateMap = MyUtil.buildDateMap(formattedDateList);

        // 5、构建表格数据
        // 数据格式形如: "tableList": [
        //                {
        //                    "label": "金蝶",
        //                    "date1": 0,
        //                    "date2": 2,
        //                    "date3": 3420,
        //                    "date4": 179,
        //                    "date5": 449,
        //                    "date6": 922,
        //                    "date7": 0,
        //                    "date8": 1822,
        //                    "date9": 0
        //                },
        //                {
        //                    "label": "聚水潭",
        //                    "date1": 2130,
        //                    "date2": 2,
        //                    "date3": 3420,
        //                    "date4": 179,
        //                    "date5": 449,
        //                    "date6": 922,
        //                    "date7": 1409,
        //                    "date8": 1822,
        //                    "date9": 1631
        //                },
        //                {
        //                    "label": "差异",
        //                    "date1": 2130,
        //                    "date2": 0,
        //                    "date3": 0,
        //                    "date4": 0,
        //                    "date5": 0,
        //                    "date6": 0,
        //                    "date7": 1409,
        //                    "date8": 0,
        //                    "date9": 1631
        //                }
        //            ]
        List<Map<String, Object>> resultList = new ArrayList<>();

        // 5-1、初始化三个 Map 来存储数据
        Map<String, Object> jdData = new LinkedHashMap<>();
        Map<String, Object> jstData = new LinkedHashMap<>();
        Map<String, Object> diffData = new LinkedHashMap<>();

        // 设置 label
        jdData.put("label", "金蝶");
        jstData.put("label", "聚水潭");
        diffData.put("label", "差异");

        // 5-2、遍历日期列表，填充数据
        int dateIndex = 1;
        for (String date : formattedDateList) {
            int jstCount = jstCountMap.getOrDefault(date, 0);
            int jdCount = jdCountMap.getOrDefault(date, 0);
            int diff = Math.abs(jstCount - jdCount);

            // 填充数据
            jdData.put("date" + dateIndex, jdCount);
            jstData.put("date" + dateIndex, jstCount);
            diffData.put("date" + dateIndex, diff);

            dateIndex++;
        }

        // 5-3、将三个 Map 添加到结果列表中
        resultList.add(jdData);
        resultList.add(jstData);
        resultList.add(diffData);
        // 5-4、设置表格数据
        jstAndJDTableDataEntry.setTableList(resultList);

        // 6、构建列数据
        // 数据格式形如:
        // [
        //                {
        //                    "label": "系统",
        //                    "prop": "label"
        //                },
        //                {
        //                    "label": "2024年07月31日",
        //                    "prop": "date1"
        //                },
        // ]
        List<Map<String, Object>> columnList = new ArrayList<>();
        // 6-1、添加 "系统" 列
        Map<String, Object> systemColumn = new LinkedHashMap<>();
        systemColumn.put("label", "系统");
        systemColumn.put("prop", "label");
        columnList.add(systemColumn);

        // 6-2、添加日期列
        for (int i = 0; i <formattedDateList.size() +1; i++) {
            Map<String, Object> dateColumn = new LinkedHashMap<>();
            // 可以根据需要修改日期格式，根据业务需要，这里先按照date1这样处理
            dateColumn.put("label", dateMap.get("date" + i));
            dateColumn.put("prop", "date" + i);
            columnList.add(dateColumn);
            dateIndex++;
        }
        // 6-3、设置表格数据
        jstAndJDTableDataEntry.setColumnList(columnList);
        jstAndJDTableDataEntryList.add(jstAndJDTableDataEntry);

        // 7、构建JSON，构建返回数据
        TableDataInfo<JstAndJDTableDataEntryBo> result = new TableDataInfo<>();
        result.setRows(jstAndJDTableDataEntryList);
        result.setMsg("查询成功");

        return result;
    }

    /**
     * 查询聚水潭-其他出库单列表以及金蝶-直接调拨单数据汇总（按日期汇总）
     * 场景分类：8、调拨-销退仓,次品仓调拨委外加工仓
     */
    @Override
    public TableDataInfo<JstAndJDTableDataEntryBo> jstAndJdSumByDateBySaleReturnTransfer() {
        List<JstAndJDTableDataEntryBo> jstAndJDTableDataEntryList =new ArrayList<>();
        JstAndJDTableDataEntryBo jstAndJDTableDataEntry = new JstAndJDTableDataEntryBo();
        // 1、查询两个平台数据
        // 1-1、查询聚水潭的数据
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw1 = Wrappers.lambdaQuery();
        // 出库类型，查询出库类型字段形如“仓库编码+仓库”的单子，其中仓库编码以“CK”开头
        lqw1.like(JstOtherOutEnterWarehouseOrder::getOutWarehouseType, "%CK%");
        lqw1.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        List<JstOtherOutEnterWarehouseOrderVo> jstOtherOutEnterWarehouseOrderList = baseMapper.selectVoList(lqw1);

        // 1-2、查询金蝶的数据
        LambdaQueryWrapper<JdDirectTransferOrder> lqw2 = Wrappers.lambdaQuery();
        lqw2.like(JdDirectTransferOrder::getReceiptNumber, "%QT%");
        List<JdDirectTransferOrderVo> jdDirectTransferOrderList = jdDirectTransferOrderMapper.selectVoList(lqw2);

        // 2、生成日期列表和处理日期
        // 2-1、生成日期列表，以聚水潭的最晚日期为截止日期取
        List<Date> dateList = jdDirectTransferOrderList.stream()
            .sorted(Comparator.comparing(JdDirectTransferOrderVo::getNewOrderDate))
            .map(JdDirectTransferOrderVo::getNewOrderDate)
            .distinct()
            .collect(Collectors.toList());

        // 2-2、将日期列表转换为字符串列表
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        List<String> dateStringList = dateList.stream()
            .map(date -> dateFormat.format(date))
            .collect(Collectors.toList());

        // 2-3、获取去重后的日期列表，并转换格式
        Set<String> formattedDateSet = dateStringList.stream()
            // 将字符串转换为 LocalDate 对象
            .map(MyUtil::parseToLocalDate)
            .sorted()
            .map(MyUtil::formatDateStringByLocalDate)
            .collect(Collectors.toSet());
        List<String> formattedDateList = new ArrayList<>(formattedDateSet);
        Collections.sort(formattedDateList, Comparator.naturalOrder());

        // 3、处理两个平台的日期
        // 3-1、处理聚水潭和金蝶的数据里面的日期
        // key 为日期，value 为数量
        Map<String, Integer> jstCountMap = jstOtherOutEnterWarehouseOrderList.stream()
            .filter(order -> order.getNewOrderDate() != null)
            .sorted(Comparator.comparing(JstOtherOutEnterWarehouseOrderVo::getNewOrderDate))
            .collect(Collectors.groupingBy(
                order -> MyUtil.formatDateStringNoPadZero(dateFormat.format(order.getNewOrderDate())),
                Collectors.summingInt(JstOtherOutEnterWarehouseOrderVo::getQuantity)
            ));

        // 3-2、处理金蝶的数据里面的日期
        // key 为日期，value 为数量
        Map<String, Integer> jdCountMap = jdDirectTransferOrderList.stream()
            .filter(order -> order.getReceiptDate() != null)
            .sorted(Comparator.comparing(JdDirectTransferOrderVo::getReceiptDate))
            .collect(Collectors.groupingBy(
                order -> MyUtil.formatDateStringPadZero(order.getReceiptDate()),
                Collectors.summingInt(JdDirectTransferOrderVo::getAllotQuantity)
            ));

        // 4、构建形如<date1,日期>的key value对，这个是前端要求
        Map<String, String> dateMap = MyUtil.buildDateMap(formattedDateList);

        // 5、构建表格数据
        // 数据格式形如: "tableList": [
        //                {
        //                    "label": "金蝶",
        //                    "date1": 0,
        //                    "date2": 2,
        //                    "date3": 3420,
        //                    "date4": 179,
        //                    "date5": 449,
        //                    "date6": 922,
        //                    "date7": 0,
        //                    "date8": 1822,
        //                    "date9": 0
        //                },
        //                {
        //                    "label": "聚水潭",
        //                    "date1": 2130,
        //                    "date2": 2,
        //                    "date3": 3420,
        //                    "date4": 179,
        //                    "date5": 449,
        //                    "date6": 922,
        //                    "date7": 1409,
        //                    "date8": 1822,
        //                    "date9": 1631
        //                },
        //                {
        //                    "label": "差异",
        //                    "date1": 2130,
        //                    "date2": 0,
        //                    "date3": 0,
        //                    "date4": 0,
        //                    "date5": 0,
        //                    "date6": 0,
        //                    "date7": 1409,
        //                    "date8": 0,
        //                    "date9": 1631
        //                }
        //            ]
        List<Map<String, Object>> resultList = new ArrayList<>();

        // 5-1、初始化三个 Map 来存储数据
        Map<String, Object> jdData = new LinkedHashMap<>();
        Map<String, Object> jstData = new LinkedHashMap<>();
        Map<String, Object> diffData = new LinkedHashMap<>();

        // 设置 label
        jdData.put("label", "金蝶");
        jstData.put("label", "聚水潭");
        diffData.put("label", "差异");

        // 5-2、遍历日期列表，填充数据
        int dateIndex = 1;
        for (String date : formattedDateList) {
            int jstCount = jstCountMap.getOrDefault(date, 0);
            int jdCount = jdCountMap.getOrDefault(date, 0);
            int diff = Math.abs(jstCount - jdCount);

            // 填充数据
            jdData.put("date" + dateIndex, jdCount);
            jstData.put("date" + dateIndex, jstCount);
            diffData.put("date" + dateIndex, diff);

            dateIndex++;
        }

        // 5-3、将三个 Map 添加到结果列表中
        resultList.add(jdData);
        resultList.add(jstData);
        resultList.add(diffData);
        // 5-4、设置表格数据
        jstAndJDTableDataEntry.setTableList(resultList);

        // 6、构建列数据
        // 数据格式形如:
        // [
        //                {
        //                    "label": "系统",
        //                    "prop": "label"
        //                },
        //                {
        //                    "label": "2024年07月31日",
        //                    "prop": "date1"
        //                },
        // ]
        List<Map<String, Object>> columnList = new ArrayList<>();
        // 6-1、添加 "系统" 列
        Map<String, Object> systemColumn = new LinkedHashMap<>();
        systemColumn.put("label", "系统");
        systemColumn.put("prop", "label");
        columnList.add(systemColumn);

        // 6-2、添加日期列
        for (int i = 1; i <formattedDateList.size() +1; i++) {
            Map<String, Object> dateColumn = new LinkedHashMap<>();
            // 可以根据需要修改日期格式，根据业务需要，这里先按照date1这样处理
            dateColumn.put("label", dateMap.get("date" + i));
            dateColumn.put("prop", "date" + i);
            columnList.add(dateColumn);
            dateIndex++;
        }
        // 6-3、设置表格数据
        jstAndJDTableDataEntry.setColumnList(columnList);
        jstAndJDTableDataEntryList.add(jstAndJDTableDataEntry);

        // 7、构建JSON，构建返回数据
        TableDataInfo<JstAndJDTableDataEntryBo> result = new TableDataInfo<>();
        result.setRows(jstAndJDTableDataEntryList);
        result.setMsg("查询成功");

        return result;
    }

    /**
     * 查询聚水潭-其他出入库单列表（金蝶缺少的数据）
     * 场景分类：6、其他出库-报废，盘亏
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> queryPageListByScrapInventoryByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        // 计算6、其他出库-报废，盘亏场景业务差异数据
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = this.calculateScrapInventoryBusinessDifference();
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperByScrapInventoryByJdDeficiency(bo, consignmentBusinessDifferenceBo);
        Page<JstOtherOutEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }



    /**
     * 计算6、其他出库-报废，盘亏场景业务差异数据
     */
    private ConsignmentBusinessDifferenceBo calculateScrapInventoryBusinessDifference() {
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = new ConsignmentBusinessDifferenceBo();
        // List<Map<key出库单号 value物料编码>>集合，最终组成要去查询金蝶缺少数据的条件
        List<Map<String, List<String>>> orderMaterialMap = new ArrayList<>();
        // key出库单号  value聚水潭其他出入库单集合
        Map<String, List<JstOtherOutEnterWarehouseOrderVo>> groupedByInnerOrderNumber1 = new HashMap<>();
        // key出库单号  value聚水潭其他出入库单对应的物料编码集合
        Map<String, List<String>> groupedByInnerOrderNumber2 = new HashMap<>();
        // key是单据编号， value是该单据编号对应的金蝶其他出库单集合
        Map<String, List<JdOtherOutWarehouseOrderVo>> groupedByReceiptNumber1 = new HashMap<>();
        // key是单据编号， value是该单据编号对应的金蝶其他出库单的物料编码集合
        Map<String, List<String>> groupedByReceiptNumber2 = new HashMap<>();
        // 聚水潭日期
        Set<String> jstOrderDateList = new HashSet<>();

        // 1、查询聚水潭其他出入库单，并且按照出库单号分组
        // 一个出库单号对应多条聚水潭其他出入库单。形如<String,List<JstOtherOutEnterWarehouseOrder>>，key是出库单号， value是该出库单号对应的聚水潭其他出入库单集合
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw1 = Wrappers.lambdaQuery();
        lqw1.in(JstOtherOutEnterWarehouseOrder::getOutWarehouseType, Arrays.asList("报废","盘亏"));
        lqw1.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        List<JstOtherOutEnterWarehouseOrderVo> jstOtherOutEnterWarehouseOrderList = baseMapper.selectVoList(lqw1);
        if (CollUtil.isNotEmpty(jstOtherOutEnterWarehouseOrderList)) {
            // 根据 outWarehouseNumber 分组
            groupedByInnerOrderNumber1 = jstOtherOutEnterWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(JstOtherOutEnterWarehouseOrderVo::getOutWarehouseNumber));

            // 第二次分组，形如Map<String,List<String>>，key是出库单号， value是该出库单号对应的聚水潭其他出入库单物料编码集合
            // 根据 outWarehouseNumber 分组，并提取 jdMaterialCode
            groupedByInnerOrderNumber2 = jstOtherOutEnterWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(
                    JstOtherOutEnterWarehouseOrderVo::getOutWarehouseNumber,
                    Collectors.mapping(JstOtherOutEnterWarehouseOrderVo::getJdMaterialCode, Collectors.toList())
                ));

        }

        // 2、查询金蝶其他出库单，并且按照单据编号分组
        LambdaQueryWrapper<JdOtherOutWarehouseOrder> lqw2 = Wrappers.lambdaQuery();
        lqw2.like(JdOtherOutWarehouseOrder::getJdOtherOutWarehouseOrderNumber, "%QT");
        List<JdOtherOutWarehouseOrderVo> jdOtherOutWarehouseOrderList = jdOtherOutWarehouseOrderMapper.selectVoList(lqw2);
        if (CollUtil.isNotEmpty(jdOtherOutWarehouseOrderList)) {
            // 单据编号形如2057362-001-QT的，需要把-001-QT这部分内容去掉，一个出库单号对应多条金蝶其他出库单
            // 形如<String,List<JdOtherOutWarehouseOrder>>，key是单据编号， value是该单据编号对应的金蝶其他出库集合
            groupedByReceiptNumber1 = jdOtherOutWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(order -> {
                    String receiptNumber = order.getJdOtherOutWarehouseOrderNumber();
                    if (receiptNumber != null && receiptNumber.contains("-")) {
                        return receiptNumber.split("-")[0];
                    }
                    return receiptNumber;
                }));
            // 第二次分组，形如Map<String,List<String>>，key是单据编号， value是该单据编号对应的金蝶其他出库单物料编码集合
            groupedByReceiptNumber2 = jdOtherOutWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(order -> {
                        String receiptNumber = order.getJdOtherOutWarehouseOrderNumber();
                        if (receiptNumber != null && receiptNumber.contains("-")) {
                            return receiptNumber.split("-")[0];
                        }
                        return receiptNumber;
                    },
                    Collectors.mapping(JdOtherOutWarehouseOrderVo::getMaterialCode, Collectors.toList())));

        }

        if (CollUtil.isNotEmpty(groupedByInnerOrderNumber2) && CollUtil.isNotEmpty(groupedByReceiptNumber2)) {
            // 3、数据比较，两次比较均以聚水潭的为准，比对出缺少的数据，组装多个Map<String, List<String>，添加进List<Map<String, List<String>>> orderMaterialMap中
            // 3-1、第一次比较，比较出金蝶不存在的出库单号，其对应的物料编码需要去第1步中的聚水潭分组数据查询，组装多个Map<String, List<String>
            // 最后添加进List<Map<String, List<String>>> orderMaterialMap中
            Set<String> jstInnerOrderNumbers = groupedByInnerOrderNumber1.keySet();
            Set<String> jdReceiptNumbers = groupedByReceiptNumber1.keySet();

            // 金蝶中不存在的出库单号
            Set<String> missingInnerOrderNumbers = jstInnerOrderNumbers.stream()
                .filter(outWarehouseNumber -> !jdReceiptNumbers.contains(outWarehouseNumber))
                .collect(Collectors.toSet());

            for (String missingInnerOrderNumber : missingInnerOrderNumbers) {
                List<String> materialCodes = groupedByInnerOrderNumber2.get(missingInnerOrderNumber);
                if (materialCodes != null) {
                    Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                    missingOrderMaterialMap.put(missingInnerOrderNumber, materialCodes);
                    orderMaterialMap.add(missingOrderMaterialMap);
                }
            }

            // 3-2、第二次比较，出库单号相同的情况下，比对缺少的物料编码，组装多个Map<String, List<String>>
            // 最后添加进List<Map<String, List<String>>> orderMaterialMap中
            for (String outWarehouseNumber : jstInnerOrderNumbers) {
                if (jdReceiptNumbers.contains(outWarehouseNumber)) {
                    List<String> jstMaterialCodes = groupedByInnerOrderNumber2.get(outWarehouseNumber);
                    List<String> jdMaterialCodes = groupedByReceiptNumber2.get(outWarehouseNumber);

                    if (jstMaterialCodes != null && jdMaterialCodes != null) {
                        // 获取聚水潭和金蝶的日期
                        List<String> jstOrderDates = groupedByInnerOrderNumber1.get(outWarehouseNumber).stream()
                            .map(JstOtherOutEnterWarehouseOrderVo::getCompareDateToJd)
                            .distinct()
                            .collect(Collectors.toList());
                        List<String> jdOrderDates = groupedByReceiptNumber1.get(outWarehouseNumber).stream()
                            .map(JdOtherOutWarehouseOrderVo::getOrderDate)
                            .distinct()
                            .collect(Collectors.toList());

                        // 比较日期
                        for (String jstOrderDate : jstOrderDates) {
                            if (!jdOrderDates.contains(jstOrderDate)) {
                                // 金蝶中不存在该日期的出库单号
                                Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                                missingOrderMaterialMap.put(outWarehouseNumber, jstMaterialCodes);
                                jstOrderDateList.add(jstOrderDate);
                                orderMaterialMap.add(missingOrderMaterialMap);
                            } else {
                                // 日期相同的情况下，比对缺少的物料编码
                                List<String> jstMaterialCodesForDate = groupedByInnerOrderNumber1.get(outWarehouseNumber).stream()
                                    .filter(order -> order.getCompareDateToJd().equals(jstOrderDate))
                                    .map(JstOtherOutEnterWarehouseOrderVo::getJdMaterialCode)
                                    .collect(Collectors.toList());
                                List<String> jdMaterialCodesForDate = groupedByReceiptNumber1.get(outWarehouseNumber).stream()
                                    .filter(order -> order.getOrderDate().equals(jstOrderDate))
                                    .map(JdOtherOutWarehouseOrderVo::getMaterialCode)
                                    .collect(Collectors.toList());

                                List<String> missingMaterialCodes = jstMaterialCodesForDate.stream()
                                    .filter(materialCode -> !jdMaterialCodesForDate.contains(materialCode))
                                    .collect(Collectors.toList());

                                if (!missingMaterialCodes.isEmpty()) {
                                    Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                                    missingOrderMaterialMap.put(outWarehouseNumber, missingMaterialCodes);
                                    orderMaterialMap.add(missingOrderMaterialMap);
                                }
                                jstOrderDateList.add(jstOrderDate);
                            }
                        }
                    }
                }
            }
        }
        consignmentBusinessDifferenceBo.setOrderMaterialMap(orderMaterialMap);
        consignmentBusinessDifferenceBo.setJstOrderDateList(jstOrderDateList);
        return consignmentBusinessDifferenceBo;
    }


    /**
     * 构建查询包装器（金蝶缺少的数据）
     * 场景分类：6、其他出库-报废，盘亏场景
     *
     * @param bo                              业务对象，包含查询参数
     * @param consignmentBusinessDifferenceBo 计算寄售业务差异bo对象
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperByScrapInventoryByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo,
                                                                                                               ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 检测bo的参数
        Boolean boParams = isAnyFieldNotEmpty(bo);
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }

        if (StrUtil.isNotBlank(bo.getIds())) {
            List<Long> idsList =
                Arrays.stream(bo.getIds().split(","))
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toList();
            lqw.in(JstOtherOutEnterWarehouseOrder::getId, idsList);
        }
        // 根据出库单号和物料编码查询（查询金蝶缺少数据），差异数据bo对象不为空且bo参数为假就导出
        if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            // 拿有差异的单号和物料编码
            Map<String, List<String>> orderMaterialMap = consignmentBusinessDifferenceBo.getOrderMaterialMap().stream()
                .flatMap(map -> map.entrySet().stream())
                .collect(Collectors.groupingBy(Map.Entry::getKey,
                    Collectors.mapping(Map.Entry::getValue, Collectors.toList())))
                .entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                    entry -> entry.getValue().stream().flatMap(List::stream).collect(Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : orderMaterialMap.entrySet()) {
                String outWarehouseNumber = entry.getKey();
                List<String> jdMaterialCodes = entry.getValue();
                lqw.or(i -> i.eq(JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, outWarehouseNumber)
                    .in(JstOtherOutEnterWarehouseOrder::getJdMaterialCode, jdMaterialCodes));
            }

//            // 拿有差异的日期查询
//            if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getJstOrderDateList())) {
//                lqw.and(i -> i.in(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, consignmentBusinessDifferenceBo.getJstOrderDateList()));
//            }
        }

        // 解决bug的查询，如果没有差异数据，那就是期望列表没数据，就返回一个假数据，避免查询所有数据
        if (CollUtil.isEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            lqw.eq(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, 0);
        }

        // 返回构建好的查询条件包装器
        return lqw;
    }

    /**
     * 导出聚水潭-其他出入库单列表（导出6、其他出库-报废，盘亏的差异数据）
     *
     * @param bo
     */
    @Override
    public List<JstOtherOutEnterWarehouseOrderVo> queryListScrapInventoryByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo) {
        // 计算6、其他出库-报废，盘亏业务差异数据
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = this.calculateScrapInventoryBusinessDifference();
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperScrapInventoryByJdDeficiency(bo, consignmentBusinessDifferenceBo);
        return baseMapper.selectVoList(lqw);
    }


    /**
     * 构建查询包装器
     *
     * @param bo 业务对象，包含查询参数
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperScrapInventoryByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo, ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 检测bo的参数
        Boolean boParams = isAnyFieldNotEmpty(bo);
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }

        if (StrUtil.isNotBlank(bo.getIds())) {
            List<Long> idsList =
                Arrays.stream(bo.getIds().split(","))
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toList();
            lqw.in(JstOtherOutEnterWarehouseOrder::getId, idsList);
        }
        // 根据出库单号和物料编码查询（查询金蝶缺少数据），差异数据bo对象不为空且bo参数为假就导出
        if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            // 拿有差异的单号和物料编码
            Map<String, List<String>> orderMaterialMap = consignmentBusinessDifferenceBo.getOrderMaterialMap().stream()
                .flatMap(map -> map.entrySet().stream())
                .collect(Collectors.groupingBy(Map.Entry::getKey,
                    Collectors.mapping(Map.Entry::getValue, Collectors.toList())))
                .entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                    entry -> entry.getValue().stream().flatMap(List::stream).collect(Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : orderMaterialMap.entrySet()) {
                String outWarehouseNumber = entry.getKey();
                List<String> jdMaterialCodes = entry.getValue();
                lqw.or(i -> i.eq(JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, outWarehouseNumber)
                    .in(JstOtherOutEnterWarehouseOrder::getJdMaterialCode, jdMaterialCodes));
            }

//            // 拿有差异的日期查询
//            if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getJstOrderDateList())) {
//                lqw.and(i -> i.in(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, consignmentBusinessDifferenceBo.getJstOrderDateList()));
//            }
        }

        // 解决bug的查询，如果没有差异数据，那就是期望列表没数据，就返回一个假数据，避免查询所有数据
        if (CollUtil.isEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            lqw.eq(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, 0);
        }

        // 返回构建好的查询条件包装器
        return lqw;
    }

    /**
     * 查询聚水潭-其他出入库单列表（金蝶缺少的数据）
     * 场景分类：7、其他入库-供应商赔付，盘盈，领用退回
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> queryPageListByClaimOrderProfitReturnByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        // 计算7、其他入库-供应商赔付，盘盈，领用退回场景业务差异数据
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = this.calculateClaimOrderProfitReturnBusinessDifference();
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperByClaimOrderProfitReturnByJdDeficiency(bo, consignmentBusinessDifferenceBo);
        Page<JstOtherOutEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    /**
     * 计算7、其他入库-供应商赔付，盘盈，领用退回场景业务差异数据
     */
    private ConsignmentBusinessDifferenceBo calculateClaimOrderProfitReturnBusinessDifference() {
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = new ConsignmentBusinessDifferenceBo();
        // List<Map<key出库单号 value物料编码>>集合，最终组成要去查询金蝶缺少数据的条件
        List<Map<String, List<String>>> orderMaterialMap = new ArrayList<>();
        // key出库单号  value聚水潭其他出入库单集合
        Map<String, List<JstOtherOutEnterWarehouseOrderVo>> groupedByInnerOrderNumber1 = new HashMap<>();
        // key出库单号  value聚水潭其他出入库单对应的物料编码集合
        Map<String, List<String>> groupedByInnerOrderNumber2 = new HashMap<>();
        // key是单据编号， value是该单据编号对应的金蝶其他入库单集合
        Map<String, List<JdOtherEnterWarehouseOrderVo>> groupedByReceiptNumber1 = new HashMap<>();
        // key是单据编号， value是该单据编号对应的金蝶其他入库单的物料编码集合
        Map<String, List<String>> groupedByReceiptNumber2 = new HashMap<>();
        // 聚水潭日期
        Set<String> jstOrderDateList = new HashSet<>();

        // 1、查询聚水潭其他出入库单，并且按照出库单号分组
        // 一个出库单号对应多条聚水潭其他出入库单。形如<String,List<JstOtherOutEnterWarehouseOrder>>，key是出库单号， value是该出库单号对应的聚水潭其他出入库单集合
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw1 = Wrappers.lambdaQuery();
        lqw1.in(JstOtherOutEnterWarehouseOrder::getOutWarehouseType, Arrays.asList("供应商赔付","盘盈","领用退回"));
        lqw1.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        List<JstOtherOutEnterWarehouseOrderVo> jstOtherOutEnterWarehouseOrderList = baseMapper.selectVoList(lqw1);
        if (CollUtil.isNotEmpty(jstOtherOutEnterWarehouseOrderList)) {
            // 根据 outWarehouseNumber 分组
            groupedByInnerOrderNumber1 = jstOtherOutEnterWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(JstOtherOutEnterWarehouseOrderVo::getOutWarehouseNumber));

            // 第二次分组，形如Map<String,List<String>>，key是出库单号， value是该出库单号对应的聚水潭其他出入库单物料编码集合
            // 根据 outWarehouseNumber 分组，并提取 jdMaterialCode
            groupedByInnerOrderNumber2 = jstOtherOutEnterWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(
                    JstOtherOutEnterWarehouseOrderVo::getOutWarehouseNumber,
                    Collectors.mapping(JstOtherOutEnterWarehouseOrderVo::getJdMaterialCode, Collectors.toList())
                ));

        }

        // 2、查询金蝶其他入库单，并且按照单据编号分组
        LambdaQueryWrapper<JdOtherEnterWarehouseOrder> lqw2 = Wrappers.lambdaQuery();
        lqw2.like(JdOtherEnterWarehouseOrder::getOtherEnterWarehouseOrderNumber, "%QT");
        List<JdOtherEnterWarehouseOrderVo> jdOtherEnterWarehouseOrderList = jdOtherEnterWarehouseOrderMapper.selectVoList(lqw2);
        if (CollUtil.isNotEmpty(jdOtherEnterWarehouseOrderList)) {
            // 单据编号形如2057362-001-QT的，需要把-001-QT这部分内容去掉，一个出库单号对应多条金蝶其他入库单
            // 形如<String,List<JdOtherEnterWarehouseOrder>>，key是单据编号， value是该单据编号对应的金蝶其他入库集合
            groupedByReceiptNumber1 = jdOtherEnterWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(order -> {
                    String receiptNumber = order.getOtherEnterWarehouseOrderNumber();
                    if (receiptNumber != null && receiptNumber.contains("-")) {
                        return receiptNumber.split("-")[0];
                    }
                    return receiptNumber;
                }));
            // 第二次分组，形如Map<String,List<String>>，key是单据编号， value是该单据编号对应的金蝶其他入库单物料编码集合
            groupedByReceiptNumber2 = jdOtherEnterWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(order -> {
                        String receiptNumber = order.getOtherEnterWarehouseOrderNumber();
                        if (receiptNumber != null && receiptNumber.contains("-")) {
                            return receiptNumber.split("-")[0];
                        }
                        return receiptNumber;
                    },
                    Collectors.mapping(JdOtherEnterWarehouseOrderVo::getMaterialCode, Collectors.toList())));

        }

        if (CollUtil.isNotEmpty(groupedByInnerOrderNumber2) && CollUtil.isNotEmpty(groupedByReceiptNumber2)) {
            // 3、数据比较，两次比较均以聚水潭的为准，比对出缺少的数据，组装多个Map<String, List<String>，添加进List<Map<String, List<String>>> orderMaterialMap中
            // 3-1、第一次比较，比较出金蝶不存在的出库单号，其对应的物料编码需要去第1步中的聚水潭分组数据查询，组装多个Map<String, List<String>
            // 最后添加进List<Map<String, List<String>>> orderMaterialMap中
            Set<String> jstInnerOrderNumbers = groupedByInnerOrderNumber1.keySet();
            Set<String> jdReceiptNumbers = groupedByReceiptNumber1.keySet();

            // 金蝶中不存在的出库单号
            Set<String> missingInnerOrderNumbers = jstInnerOrderNumbers.stream()
                .filter(outWarehouseNumber -> !jdReceiptNumbers.contains(outWarehouseNumber))
                .collect(Collectors.toSet());

            for (String missingInnerOrderNumber : missingInnerOrderNumbers) {
                List<String> materialCodes = groupedByInnerOrderNumber2.get(missingInnerOrderNumber);
                if (materialCodes != null) {
                    Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                    missingOrderMaterialMap.put(missingInnerOrderNumber, materialCodes);
                    orderMaterialMap.add(missingOrderMaterialMap);
                }
            }

            // 3-2、第二次比较，出库单号相同的情况下，比对缺少的物料编码，组装多个Map<String, List<String>>
            // 最后添加进List<Map<String, List<String>>> orderMaterialMap中
            for (String outWarehouseNumber : jstInnerOrderNumbers) {
                if (jdReceiptNumbers.contains(outWarehouseNumber)) {
                    List<String> jstMaterialCodes = groupedByInnerOrderNumber2.get(outWarehouseNumber);
                    List<String> jdMaterialCodes = groupedByReceiptNumber2.get(outWarehouseNumber);

                    if (jstMaterialCodes != null && jdMaterialCodes != null) {
                        // 获取聚水潭和金蝶的日期
                        List<String> jstOrderDates = groupedByInnerOrderNumber1.get(outWarehouseNumber).stream()
                            .map(JstOtherOutEnterWarehouseOrderVo::getCompareDateToJd)
                            .distinct()
                            .collect(Collectors.toList());
                        List<String> jdOrderDates = groupedByReceiptNumber1.get(outWarehouseNumber).stream()
                            .map(JdOtherEnterWarehouseOrderVo::getOtherEnterWarehouseOrderDate)
                            .distinct()
                            .collect(Collectors.toList());

                        // 比较日期
                        for (String jstOrderDate : jstOrderDates) {
                            if (!jdOrderDates.contains(jstOrderDate)) {
                                // 金蝶中不存在该日期的出库单号
                                Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                                missingOrderMaterialMap.put(outWarehouseNumber, jstMaterialCodes);
                                jstOrderDateList.add(jstOrderDate);
                                orderMaterialMap.add(missingOrderMaterialMap);
                            } else {
                                // 日期相同的情况下，比对缺少的物料编码
                                List<String> jstMaterialCodesForDate = groupedByInnerOrderNumber1.get(outWarehouseNumber).stream()
                                    .filter(order -> order.getCompareDateToJd().equals(jstOrderDate))
                                    .map(JstOtherOutEnterWarehouseOrderVo::getJdMaterialCode)
                                    .collect(Collectors.toList());
                                List<String> jdMaterialCodesForDate = groupedByReceiptNumber1.get(outWarehouseNumber).stream()
                                    .filter(order -> order.getOtherEnterWarehouseOrderDate().equals(jstOrderDate))
                                    .map(JdOtherEnterWarehouseOrderVo::getMaterialCode)
                                    .collect(Collectors.toList());

                                List<String> missingMaterialCodes = jstMaterialCodesForDate.stream()
                                    .filter(materialCode -> !jdMaterialCodesForDate.contains(materialCode))
                                    .collect(Collectors.toList());

                                if (!missingMaterialCodes.isEmpty()) {
                                    Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                                    missingOrderMaterialMap.put(outWarehouseNumber, missingMaterialCodes);
                                    orderMaterialMap.add(missingOrderMaterialMap);
                                }
                                jstOrderDateList.add(jstOrderDate);
                            }
                        }
                    }
                }
            }
        }
        consignmentBusinessDifferenceBo.setOrderMaterialMap(orderMaterialMap);
        consignmentBusinessDifferenceBo.setJstOrderDateList(jstOrderDateList);
        return consignmentBusinessDifferenceBo;
    }


    /**
     * 构建查询包装器（金蝶缺少的数据）
     * 场景分类：7、其他入库-供应商赔付，盘盈，领用退回场景
     *
     * @param bo                              业务对象，包含查询参数
     * @param consignmentBusinessDifferenceBo 计算业务差异bo对象
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperByClaimOrderProfitReturnByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo,
                                                                                                               ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 检测bo的参数
        Boolean boParams = isAnyFieldNotEmpty(bo);
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }

        if (StrUtil.isNotBlank(bo.getIds())) {
            List<Long> idsList =
                Arrays.stream(bo.getIds().split(","))
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toList();
            lqw.in(JstOtherOutEnterWarehouseOrder::getId, idsList);
        }
        // 根据出库单号和物料编码查询（查询金蝶缺少数据），差异数据bo对象不为空且bo参数为假就导出
        if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            // 拿有差异的单号和物料编码
            Map<String, List<String>> orderMaterialMap = consignmentBusinessDifferenceBo.getOrderMaterialMap().stream()
                .flatMap(map -> map.entrySet().stream())
                .collect(Collectors.groupingBy(Map.Entry::getKey,
                    Collectors.mapping(Map.Entry::getValue, Collectors.toList())))
                .entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                    entry -> entry.getValue().stream().flatMap(List::stream).collect(Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : orderMaterialMap.entrySet()) {
                String outWarehouseNumber = entry.getKey();
                List<String> jdMaterialCodes = entry.getValue();
                lqw.or(i -> i.eq(JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, outWarehouseNumber)
                    .in(JstOtherOutEnterWarehouseOrder::getJdMaterialCode, jdMaterialCodes));
            }

//            // 拿有差异的日期查询
//            if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getJstOrderDateList())) {
//                lqw.and(i -> i.in(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, consignmentBusinessDifferenceBo.getJstOrderDateList()));
//            }
        }

        // 解决bug的查询，如果没有差异数据，那就是期望列表没数据，就返回一个假数据，避免查询所有数据
        if (CollUtil.isEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            lqw.eq(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, 0);
        }

        // 返回构建好的查询条件包装器
        return lqw;
    }

    /**
     * 导出聚水潭-其他出入库单列表（导出7、其他入库-供应商赔付，盘盈，领用退回的差异数据） TODO 待对接
     *
     * @param bo
     */
    @Override
    public List<JstOtherOutEnterWarehouseOrderVo> queryListClaimOrderProfitReturnByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo) {
        // 计算7、其他入库-供应商赔付，盘盈，领用退回业务差异数据
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = this.calculateClaimOrderProfitReturnBusinessDifference();
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperClaimOrderProfitReturnByJdDeficiency(bo, consignmentBusinessDifferenceBo);
        return baseMapper.selectVoList(lqw);
    }


    /**
     * 构建查询包装器（金蝶缺少的数据）
     * 场景分类：7、其他入库-供应商赔付，盘盈，领用退回场景
     *
     * @param bo                              业务对象，包含查询参数
     * @param consignmentBusinessDifferenceBo 计算业务差异bo对象
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperClaimOrderProfitReturnByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo,
                                                                                                                       ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 检测bo的参数
        Boolean boParams = isAnyFieldNotEmpty(bo);
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }

        if (StrUtil.isNotBlank(bo.getIds())) {
            List<Long> idsList =
                Arrays.stream(bo.getIds().split(","))
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toList();
            lqw.in(JstOtherOutEnterWarehouseOrder::getId, idsList);
        }
        // 根据出库单号和物料编码查询（查询金蝶缺少数据），差异数据bo对象不为空且bo参数为假就导出
        if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            // 拿有差异的单号和物料编码
            Map<String, List<String>> orderMaterialMap = consignmentBusinessDifferenceBo.getOrderMaterialMap().stream()
                .flatMap(map -> map.entrySet().stream())
                .collect(Collectors.groupingBy(Map.Entry::getKey,
                    Collectors.mapping(Map.Entry::getValue, Collectors.toList())))
                .entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                    entry -> entry.getValue().stream().flatMap(List::stream).collect(Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : orderMaterialMap.entrySet()) {
                String outWarehouseNumber = entry.getKey();
                List<String> jdMaterialCodes = entry.getValue();
                lqw.or(i -> i.eq(JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, outWarehouseNumber)
                    .in(JstOtherOutEnterWarehouseOrder::getJdMaterialCode, jdMaterialCodes));
            }

//            // 拿有差异的日期查询
//            if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getJstOrderDateList())) {
//                lqw.and(i -> i.in(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, consignmentBusinessDifferenceBo.getJstOrderDateList()));
//            }
        }

        // 解决bug的查询，如果没有差异数据，那就是期望列表没数据，就返回一个假数据，避免查询所有数据
        if (CollUtil.isEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            lqw.eq(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, 0);
        }

        // 返回构建好的查询条件包装器
        return lqw;
    }

    /**
     * 查询聚水潭-其他出入库单列表（金蝶缺少的数据）
     * 场景分类：8、调拨-销退仓,次品仓调拨委外加工仓
     */
    @Override
    public TableDataInfo<JstOtherOutEnterWarehouseOrderVo> queryPageListBySaleReturnTransferByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo, PageQuery pageQuery) {
        // 计算8、调拨-销退仓,次品仓调拨委外加工仓场景业务差异数据
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = this.calculateSaleReturnTransferBusinessDifference();
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperBySaleReturnTransferByJdDeficiency(bo, consignmentBusinessDifferenceBo);
        Page<JstOtherOutEnterWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    /**
     * 计算8、调拨-销退仓,次品仓调拨委外加工仓场景业务差异数据
     */
    private ConsignmentBusinessDifferenceBo calculateSaleReturnTransferBusinessDifference() {
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = new ConsignmentBusinessDifferenceBo();
        // List<Map<key出库单号 value物料编码>>集合，最终组成要去查询金蝶缺少数据的条件
        List<Map<String, List<String>>> orderMaterialMap = new ArrayList<>();
        // key出库单号  value聚水潭其他出入库单集合
        Map<String, List<JstOtherOutEnterWarehouseOrderVo>> groupedByInnerOrderNumber1 = new HashMap<>();
        // key出库单号  value聚水潭其他出入库单对应的物料编码集合
        Map<String, List<String>> groupedByInnerOrderNumber2 = new HashMap<>();
        // key是单据编号， value是该单据编号对应的金蝶直接调拨单集合
        Map<String, List<JdDirectTransferOrderVo>> groupedByReceiptNumber1 = new HashMap<>();
        // key是单据编号， value是该单据编号对应的金蝶其他入库单的物料编码集合
        Map<String, List<String>> groupedByReceiptNumber2 = new HashMap<>();
        // 聚水潭日期
        Set<String> jstOrderDateList = new HashSet<>();

        // 1、查询聚水潭其他出入库单，并且按照出库单号分组
        // 一个出库单号对应多条聚水潭其他出入库单。形如<String,List<JstOtherOutEnterWarehouseOrder>>，key是出库单号， value是该出库单号对应的聚水潭其他出入库单集合
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw1 = Wrappers.lambdaQuery();
        // 出库类型，查询出库类型字段形如“仓库编码+仓库”的单子，其中仓库编码以“CK”开头
        lqw1.like(JstOtherOutEnterWarehouseOrder::getOutWarehouseType, "%CK%");
        lqw1.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        List<JstOtherOutEnterWarehouseOrderVo> jstOtherOutEnterWarehouseOrderList = baseMapper.selectVoList(lqw1);
        if (CollUtil.isNotEmpty(jstOtherOutEnterWarehouseOrderList)) {
            // 根据 outWarehouseNumber 分组
            groupedByInnerOrderNumber1 = jstOtherOutEnterWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(JstOtherOutEnterWarehouseOrderVo::getOutWarehouseNumber));

            // 第二次分组，形如Map<String,List<String>>，key是出库单号， value是该出库单号对应的聚水潭其他出入库单物料编码集合
            // 根据 outWarehouseNumber 分组，并提取 jdMaterialCode
            groupedByInnerOrderNumber2 = jstOtherOutEnterWarehouseOrderList.stream()
                .collect(Collectors.groupingBy(
                    JstOtherOutEnterWarehouseOrderVo::getOutWarehouseNumber,
                    Collectors.mapping(JstOtherOutEnterWarehouseOrderVo::getJdMaterialCode, Collectors.toList())
                ));

        }

        // 2、查询金蝶直接调拨单，并且按照单据编号分组
        LambdaQueryWrapper<JdDirectTransferOrder> lqw2 = Wrappers.lambdaQuery();
        lqw2.like(JdDirectTransferOrder::getReceiptNumber, "%QT");
        List<JdDirectTransferOrderVo> jdDirectTransferOrderList = jdDirectTransferOrderMapper.selectVoList(lqw2);
        if (CollUtil.isNotEmpty(jdDirectTransferOrderList)) {
            // 单据编号形如2057362-001-QT的，需要把-001-QT这部分内容去掉，一个出库单号对应多条金蝶直接调拨单
            // 形如<String,List<JdOtherEnterWarehouseOrder>>，key是单据编号， value是该单据编号对应的金蝶直接调拨集合
            groupedByReceiptNumber1 = jdDirectTransferOrderList.stream()
                .collect(Collectors.groupingBy(order -> {
                    String receiptNumber = order.getReceiptNumber();
                    if (receiptNumber != null && receiptNumber.contains("-")) {
                        return receiptNumber.split("-")[0];
                    }
                    return receiptNumber;
                }));
            // 第二次分组，形如Map<String,List<String>>，key是单据编号， value是该单据编号对应的金蝶直接调拨单物料编码集合
            groupedByReceiptNumber2 = jdDirectTransferOrderList.stream()
                .collect(Collectors.groupingBy(order -> {
                        String receiptNumber = order.getReceiptNumber();
                        if (receiptNumber != null && receiptNumber.contains("-")) {
                            return receiptNumber.split("-")[0];
                        }
                        return receiptNumber;
                    },
                    Collectors.mapping(JdDirectTransferOrderVo::getMaterialCode, Collectors.toList())));

        }

        if (CollUtil.isNotEmpty(groupedByInnerOrderNumber2) && CollUtil.isNotEmpty(groupedByReceiptNumber2)) {
            // 3、数据比较，两次比较均以聚水潭的为准，比对出缺少的数据，组装多个Map<String, List<String>，添加进List<Map<String, List<String>>> orderMaterialMap中
            // 3-1、第一次比较，比较出金蝶不存在的出库单号，其对应的物料编码需要去第1步中的聚水潭分组数据查询，组装多个Map<String, List<String>
            // 最后添加进List<Map<String, List<String>>> orderMaterialMap中
            Set<String> jstInnerOrderNumbers = groupedByInnerOrderNumber1.keySet();
            Set<String> jdReceiptNumbers = groupedByReceiptNumber1.keySet();

            // 金蝶中不存在的出库单号
            Set<String> missingInnerOrderNumbers = jstInnerOrderNumbers.stream()
                .filter(outWarehouseNumber -> !jdReceiptNumbers.contains(outWarehouseNumber))
                .collect(Collectors.toSet());

            for (String missingInnerOrderNumber : missingInnerOrderNumbers) {
                List<String> materialCodes = groupedByInnerOrderNumber2.get(missingInnerOrderNumber);
                if (materialCodes != null) {
                    Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                    missingOrderMaterialMap.put(missingInnerOrderNumber, materialCodes);
                    orderMaterialMap.add(missingOrderMaterialMap);
                }
            }

            // 3-2、第二次比较，出库单号相同的情况下，比对缺少的物料编码，组装多个Map<String, List<String>>
            // 最后添加进List<Map<String, List<String>>> orderMaterialMap中
            for (String outWarehouseNumber : jstInnerOrderNumbers) {
                if (jdReceiptNumbers.contains(outWarehouseNumber)) {
                    List<String> jstMaterialCodes = groupedByInnerOrderNumber2.get(outWarehouseNumber);
                    List<String> jdMaterialCodes = groupedByReceiptNumber2.get(outWarehouseNumber);

                    if (jstMaterialCodes != null && jdMaterialCodes != null) {
                        // 获取聚水潭和金蝶的日期
                        List<String> jstOrderDates = groupedByInnerOrderNumber1.get(outWarehouseNumber).stream()
                            .map(JstOtherOutEnterWarehouseOrderVo::getCompareDateToJd)
                            .distinct()
                            .collect(Collectors.toList());
                        List<String> jdOrderDates = groupedByReceiptNumber1.get(outWarehouseNumber).stream()
                            .map(JdDirectTransferOrderVo::getReceiptDate)
                            .distinct()
                            .collect(Collectors.toList());

                        // 比较日期
                        for (String jstOrderDate : jstOrderDates) {
                            if (!jdOrderDates.contains(jstOrderDate)) {
                                // 金蝶中不存在该日期的出库单号
                                Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                                missingOrderMaterialMap.put(outWarehouseNumber, jstMaterialCodes);
                                jstOrderDateList.add(jstOrderDate);
                                orderMaterialMap.add(missingOrderMaterialMap);
                            } else {
                                // 日期相同的情况下，比对缺少的物料编码
                                List<String> jstMaterialCodesForDate = groupedByInnerOrderNumber1.get(outWarehouseNumber).stream()
                                    .filter(order -> order.getCompareDateToJd().equals(jstOrderDate))
                                    .map(JstOtherOutEnterWarehouseOrderVo::getJdMaterialCode)
                                    .collect(Collectors.toList());
                                List<String> jdMaterialCodesForDate = groupedByReceiptNumber1.get(outWarehouseNumber).stream()
                                    .filter(order -> order.getReceiptDate().equals(jstOrderDate))
                                    .map(JdDirectTransferOrderVo::getMaterialCode)
                                    .collect(Collectors.toList());

                                List<String> missingMaterialCodes = jstMaterialCodesForDate.stream()
                                    .filter(materialCode -> !jdMaterialCodesForDate.contains(materialCode))
                                    .collect(Collectors.toList());

                                if (!missingMaterialCodes.isEmpty()) {
                                    Map<String, List<String>> missingOrderMaterialMap = new HashMap<>();
                                    missingOrderMaterialMap.put(outWarehouseNumber, missingMaterialCodes);
                                    orderMaterialMap.add(missingOrderMaterialMap);
                                }
                                jstOrderDateList.add(jstOrderDate);
                            }
                        }
                    }
                }
            }
        }
        consignmentBusinessDifferenceBo.setOrderMaterialMap(orderMaterialMap);
        consignmentBusinessDifferenceBo.setJstOrderDateList(jstOrderDateList);
        return consignmentBusinessDifferenceBo;
    }

    /**
     * 构建查询包装器（金蝶缺少的数据）
     * 场景分类：8、调拨-销退仓,次品仓调拨委外加工仓退回场景
     *
     * @param bo                              业务对象，包含查询参数
     * @param consignmentBusinessDifferenceBo 计算业务差异bo对象
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperBySaleReturnTransferByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo,
                                                                                                                     ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 检测bo的参数
        Boolean boParams = isAnyFieldNotEmpty(bo);
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }

        if (StrUtil.isNotBlank(bo.getIds())) {
            List<Long> idsList =
                Arrays.stream(bo.getIds().split(","))
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toList();
            lqw.in(JstOtherOutEnterWarehouseOrder::getId, idsList);
        }
        // 根据出库单号和物料编码查询（查询金蝶缺少数据），差异数据bo对象不为空且bo参数为假就导出
        if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            // 拿有差异的单号和物料编码
            Map<String, List<String>> orderMaterialMap = consignmentBusinessDifferenceBo.getOrderMaterialMap().stream()
                .flatMap(map -> map.entrySet().stream())
                .collect(Collectors.groupingBy(Map.Entry::getKey,
                    Collectors.mapping(Map.Entry::getValue, Collectors.toList())))
                .entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                    entry -> entry.getValue().stream().flatMap(List::stream).collect(Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : orderMaterialMap.entrySet()) {
                String outWarehouseNumber = entry.getKey();
                List<String> jdMaterialCodes = entry.getValue();
                lqw.or(i -> i.eq(JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, outWarehouseNumber)
                    .in(JstOtherOutEnterWarehouseOrder::getJdMaterialCode, jdMaterialCodes));
            }

//            // 拿有差异的日期查询
//            if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getJstOrderDateList())) {
//                lqw.and(i -> i.in(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, consignmentBusinessDifferenceBo.getJstOrderDateList()));
//            }
        }

        // 解决bug的查询，如果没有差异数据，那就是期望列表没数据，就返回一个假数据，避免查询所有数据
        if (CollUtil.isEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            lqw.eq(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, 0);
        }

        // 返回构建好的查询条件包装器
        return lqw;
    }


    /**
     * 导出聚水潭-其他出入库单列表（导出8、调拨-销退仓,次品仓调拨委外加工仓的差异数据）
     *
     * @param bo
     */
    @Override
    public List<JstOtherOutEnterWarehouseOrderVo> queryListSaleReturnTransferByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo) {
        // 计算8、调拨-销退仓,次品仓调拨委外加工仓业务差异数据
        ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo = this.calculateSaleReturnTransferBusinessDifference();
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = buildQueryWrapperSaleReturnTransferByJdDeficiency(bo, consignmentBusinessDifferenceBo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 构建查询包装器（金蝶缺少的数据）
     * 场景分类：8、调拨-销退仓,次品仓调拨委外加工仓退回场景
     *
     * @param bo                              业务对象，包含查询参数
     * @param consignmentBusinessDifferenceBo 计算业务差异bo对象
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> buildQueryWrapperSaleReturnTransferByJdDeficiency(JstOtherOutEnterWarehouseOrderBo bo,
                                                                                                                   ConsignmentBusinessDifferenceBo consignmentBusinessDifferenceBo) {
        // 获取业务对象中携带的额外参数，用于扩展查询条件
        Map<String, Object> params = bo.getParams();
        // 初始化LambdaQueryWrapper，用于构建查询条件
        LambdaQueryWrapper<JstOtherOutEnterWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 检测bo的参数
        Boolean boParams = isAnyFieldNotEmpty(bo);
        // 根据业务对象的属性，动态设置查询条件
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseNumber()), JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, bo.getOutWarehouseNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getBillDate()), JstOtherOutEnterWarehouseOrder::getBillDate, bo.getBillDate());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JstOtherOutEnterWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBillCreator()), JstOtherOutEnterWarehouseOrder::getBillCreator, bo.getBillCreator());
        lqw.eq(StringUtils.isNotBlank(bo.getBillRemark()), JstOtherOutEnterWarehouseOrder::getBillRemark, bo.getBillRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorWarehouseOwner()), JstOtherOutEnterWarehouseOrder::getOperatorWarehouseOwner, bo.getOperatorWarehouseOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getProductNumber()), JstOtherOutEnterWarehouseOrder::getProductNumber, bo.getProductNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getColorSku()), JstOtherOutEnterWarehouseOrder::getColorSku, bo.getColorSku());
        lqw.eq(StringUtils.isNotBlank(bo.getStyleNumber()), JstOtherOutEnterWarehouseOrder::getStyleNumber, bo.getStyleNumber());
        lqw.eq(bo.getQuantity() != null, JstOtherOutEnterWarehouseOrder::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getOutWarehouseType()), JstOtherOutEnterWarehouseOrder::getOutWarehouseType, bo.getOutWarehouseType());
        lqw.eq(bo.getBillCreateTime() != null, JstOtherOutEnterWarehouseOrder::getBillCreateTime, bo.getBillCreateTime());
        lqw.eq(JstOtherOutEnterWarehouseOrder::getDataSign, "0");
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JstOtherOutEnterWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }

        if (StrUtil.isNotBlank(bo.getIds())) {
            List<Long> idsList =
                Arrays.stream(bo.getIds().split(","))
                    .map(String::trim)
                    .map(Long::parseLong)
                    .toList();
            lqw.in(JstOtherOutEnterWarehouseOrder::getId, idsList);
        }
        // 根据出库单号和物料编码查询（查询金蝶缺少数据），差异数据bo对象不为空且bo参数为假就导出
        if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            // 拿有差异的单号和物料编码
            Map<String, List<String>> orderMaterialMap = consignmentBusinessDifferenceBo.getOrderMaterialMap().stream()
                .flatMap(map -> map.entrySet().stream())
                .collect(Collectors.groupingBy(Map.Entry::getKey,
                    Collectors.mapping(Map.Entry::getValue, Collectors.toList())))
                .entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                    entry -> entry.getValue().stream().flatMap(List::stream).collect(Collectors.toList())));
            for (Map.Entry<String, List<String>> entry : orderMaterialMap.entrySet()) {
                String outWarehouseNumber = entry.getKey();
                List<String> jdMaterialCodes = entry.getValue();
                lqw.or(i -> i.eq(JstOtherOutEnterWarehouseOrder::getOutWarehouseNumber, outWarehouseNumber)
                    .in(JstOtherOutEnterWarehouseOrder::getJdMaterialCode, jdMaterialCodes));
            }

//            // 拿有差异的日期查询
//            if (CollUtil.isNotEmpty(consignmentBusinessDifferenceBo.getJstOrderDateList())) {
//                lqw.and(i -> i.in(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, consignmentBusinessDifferenceBo.getJstOrderDateList()));
//            }
        }

        // 解决bug的查询，如果没有差异数据，那就是期望列表没数据，就返回一个假数据，避免查询所有数据
        if (CollUtil.isEmpty(consignmentBusinessDifferenceBo.getOrderMaterialMap()) && !boParams) {
            lqw.eq(JstOtherOutEnterWarehouseOrder::getCompareDateToJd, 0);
        }

        // 返回构建好的查询条件包装器
        return lqw;
    }


    /**
     * 校验实体类的每一项，如果任意一个参数值不为空，则返回 true，否则返回 false
     * @return boolean
     */
    public boolean isAnyFieldNotEmpty(JstOtherOutEnterWarehouseOrderBo bo) {
        return bo.getId() != null ||
            (bo.getIds() != null && !bo.getIds().isEmpty()) ||
            (bo.getOutWarehouseNumber() != null && !bo.getOutWarehouseNumber().isEmpty()) ||
            (bo.getBillDate() != null && !bo.getBillDate().isEmpty()) ||
            (bo.getWarehouseName() != null && !bo.getWarehouseName().isEmpty()) ||
            (bo.getBillCreator() != null && !bo.getBillCreator().isEmpty()) ||
            (bo.getBillRemark() != null && !bo.getBillRemark().isEmpty()) ||
            (bo.getOperatorWarehouseOwner() != null && !bo.getOperatorWarehouseOwner().isEmpty()) ||
            (bo.getProductNumber() != null && !bo.getProductNumber().isEmpty()) ||
            (bo.getColorSku() != null && !bo.getColorSku().isEmpty()) ||
            (bo.getStyleNumber() != null && !bo.getStyleNumber().isEmpty()) ||
            bo.getQuantity() != null ||
            (bo.getOutWarehouseType() != null && !bo.getOutWarehouseType().isEmpty()) ||
            (bo.getBillCreateTime() != null && !bo.getBillCreateTime().isEmpty()) ||
            (bo.getOutWarehouseTypeList() != null && !bo.getOutWarehouseTypeList().isEmpty()) ||
            bo.getNewOrderDate() != null ||
            (bo.getJdMaterialCode() != null && !bo.getJdMaterialCode().isEmpty()) ||
            (bo.getCompareDateToJd() != null && !bo.getCompareDateToJd().isEmpty());
    }



}
